﻿package
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	
	dynamic public class pirateMain extends MovieClip
	{
			//number of men on the play field
			private var manCt:Number = 9;
			
			//starting positions of men
			private var manOrigX:Number = 50;
			private var manOrigY:Number = 50;
			
			//current positions of men
			private var currManPosX:Number = manOrigX;
			private var currManPosY:Number = manOrigY;
			
			//a players roll
			private var roll:Number = 0;
			
			//total value of selected men 
			private var totalSelected:Number = 0;
			
			//not needed
			//number of points left on the board
			private var remainingTotal:Number = 45;
			
			//not needed
			//number of men left
			private var remainingMen:Number = 9;
			
			//Holds the instances of the men
			private var manArray:Array = new Array();
			
			//holds the value for the men
			//should make this into the second dimension of above array
			private var manArrayValues:Array = new Array();
			
			//Records the currently selected men
			private var manSelected:Array = new Array();
			
			//not needed
			//Records the eliminated men
			private var manEliminated:Array = new Array();
			
			//not needed
			//cannot make another turn
			private var gameOver:Boolean = false;
			
			//is true if there is a move still available
			private var availableMove:Boolean = false;
			
			private var score_mc:TextField;			
			
			private var slay_mc:MovieClip = new Slay();
			
			private var random_mc:MovieClip = new Roll();			
							
			private var die1Roll:Number = 0;
			private var die2Roll:Number = 0;
			
			private var endScore:Number = 0;
			
			
			public function pirateMain():void
			{							
				score_mc = new TextField();// = new TextField();
				score_mc.width = 150;
				score_mc.y = 300;
				score_mc.x = 300;
				addChild(score_mc);
				
				//don't put the score up until u win
				score_mc.alpha = 0;
				
				//zero out men array records
				for(var p:int = 0; 10 > p; p++)
				{
					manSelected[p] = false;
					manEliminated[p] = false;
				}
				
				//create all men and initialize their variables
				for(var q:int = 1; 10 > q; q++)
				{
					manArray[q] = new Man;
					manArray[q].x = currManPosX;
					manArray[q].y = currManPosY;
					manArray[q].buttonMode = true;
					addChild(manArray[q]);
					manArray[q].gotoAndStop(q);
					currManPosX += manOrigX;
					manArrayValues[q] = q;
					//trace(manArrayValues[q]);
				}
				
							//need a function to roll dice
				
				random_mc.x = 10;
				random_mc.y = 300;
				addChild(random_mc);
				random_mc.addEventListener(MouseEvent.CLICK, onClick);

			
				slay_mc.x = 45;
				slay_mc.y = 225;
				addChild(slay_mc);

			}


						
			
			//selects or deselects men when clicked on
			private function selectMan(event:MouseEvent):void
			{
				for(var g:int = 0; 10 > g; g++)
				{
					if(event.currentTarget == manArray[g])
					manSelected[g] = !manSelected[g];
			
					//trace(manSelected[g], g);
				}
				
				addTotal();
				//trace(totalSelected);
			}
			
			//add the total score for all the selected men
			private function addTotal():void
			{
				totalSelected = 0;
				
				//return all men to starting position
				for(var t:int = 1; 10 > t; t++)
				{
					manArray[t].y = manOrigY;
				}
				
				//then put all the old and newly selected man back
				for(var h:int = 0; 10 > h; h++)
				{
					if(manSelected[h] == true)
					{
						manArray[h].y = 200;
						totalSelected += h;
					}
				}	
				score_mc.text = String(totalSelected);
			}
			
			//check to see if the selected men match the roll of your die after you click slay
			private function verifyCorrect(event:MouseEvent):void
			{	
				//create the slay button
				slay_mc.buttonMode = true;
				slay_mc.removeEventListener(MouseEvent.CLICK, verifyCorrect)
				trace("slayed");
				//if correct, perform reaction
				if(roll == totalSelected)
				{
					//perform reaction to player completing a turn
					for(var t:int = 0; 10 > t; t++)
					{
						if(manSelected[t] == true)
						{
							removeChild(manArray[t]);
							--remainingMen;
							manArrayValues[t] = 0;
							die1_mc.play();
							die2_mc.play();
							random_mc.addEventListener(MouseEvent.CLICK, onClick);
						}
					}
					
					//Remove event listeners for all men
					for (var m:int = 1; 10 > m; m++)
					{
						manArray[m].removeEventListener(MouseEvent.CLICK, selectMan);
					}
					
					remainingTotal -= roll;
					roll = 0;
			
					for(var w:int = 1; 10 > w; w++)
					{
						manSelected[w] = false;
					}
				}
				else
				{
					for(var q:int = 1; 10 > q; q++)
					{
						if(manSelected[q] == true)
						{
							manSelected[q] = false;
							manArray[q].y = 50;
						}
					}
					addTotal();
				}
				var win:Boolean = false
				for(var h:Number = 1; h <= manCt; h++)
				{
					if(manArrayValues[h] == 0)
					{
						win = true;
					}
					else
					{
						win = false;
						break;
					}
				}
				if(win)
				{
					score_mc.alpha = 1;
					score_mc.text = "You Win! Score: 0";
				}
			}
			
			var _temp:Number = 0;
			private function checkEnd():Boolean
			{	
				for(var i:Number = 1; i <= manCt; i++)
				{
					for(var k:Number = 1; k <= manCt; k++)
					{
						if(manArrayValues[i] != manArrayValues[k] && manArrayValues[i] != 0)
						{
							_temp = manArrayValues[i] + manArrayValues[k];
							if(roll == _temp)
							return false;
						}
					}
				}
				return true;
			}
				//var _temp:int = 0;
			//	for(var i:int = 1; i <= remainingMen; i++)
			//	{
			//		if(!manEliminated[i])
			//		{
			//			_temp = manEliminated[i] + manEliminated[i+1];
			//			if(_temp == roll)
			//			gameOver = false;
			//		}
			//	}
			//	if(gameOver == true)
			//	trace('game over');
			
			

			
			private function onClick(event:MouseEvent):void
			{
				random_mc.removeEventListener(MouseEvent.CLICK, onClick);
				
				//create event listeners for all men
				for (var m:int = 1; 10 > m; m++)
				{
					manArray[m].addEventListener(MouseEvent.CLICK, selectMan);
				}
				
				//create the slay button
				slay_mc.buttonMode = true;
				slay_mc.addEventListener(MouseEvent.CLICK, verifyCorrect)
			
				die1Roll = Math.ceil(Math.random() * 6);
				die2Roll = Math.ceil(Math.random() * 6);
				
				die1_mc.gotoAndStop(die1Roll);
				die2_mc.gotoAndStop(die2Roll);
				
				roll = die1Roll + die2Roll;
						
				if(checkEnd())
				{
					endScore = 0;
					for(var p:Number = 1; p <= manCt; p++)
						{
							endScore += manArrayValues[p];
							trace(manArrayValues[p]);
						}
					score_mc.alpha = 1;
					score_mc.text = "Game Over, score is " +  String(endScore);
					slay_mc.removeEventListener(MouseEvent.CLICK, verifyCorrect);
					
				}
			}
			
			var thisSum:int = 0;
			private function sum(curSum,diceRoll,numMen):Boolean
			{
				if(numMen < 1)
				return false;
				
				thisSum = curSum + numMen;
				
				if(thisSum == diceRoll && !manEliminated[numMen-1])
				return true;
				
				if(!manEliminated[numMen-1])
				{
					if(sum(thisSum,diceRoll,numMen-1))
					return true;
				}
					
				if(sum(thisSum - numMen,diceRoll,numMen-1))
				return true;
				
				return false;
			}
			
			
			
			
			//need a function to check to see if there are any more available moves
			//allow the player to keep making turns until all men are slayed or no more moves
			//timer function that plays a tick at every 4th beat
			//function that keeps a noise playing (man screaming) while he is selected
			
			
			//onEnterFrame() ????????
			//addEventListener(Event.ENTER_FRAME, myFunction);
			//function myFunction(event:Event)
			//{
			//	manArray[1].x += 1;
			//}
	}
}